<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>transaction.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_db_transaction.js.html">transaction.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Wrapper for an IndexedDB transaction.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.db.Transaction&#39;);
<a name="line22"></a>goog.provide(&#39;goog.db.Transaction.TransactionMode&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line25"></a>goog.require(&#39;goog.db.Error&#39;);
<a name="line26"></a>goog.require(&#39;goog.db.ObjectStore&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * Creates a new transaction. Transactions contain methods for accessing object
<a name="line34"></a> * stores and are created from the database object. Should not be created
<a name="line35"></a> * directly, open a database and call createTransaction on it.
<a name="line36"></a> * @see goog.db.IndexedDb#createTransaction
<a name="line37"></a> *
<a name="line38"></a> * @param {!IDBTransaction} tx IndexedDB transaction to back this wrapper.
<a name="line39"></a> * @param {!goog.db.IndexedDb} db The database that this transaction modifies.
<a name="line40"></a> * @constructor
<a name="line41"></a> * @extends {goog.events.EventTarget}
<a name="line42"></a> */
<a name="line43"></a>goog.db.Transaction = function(tx, db) {
<a name="line44"></a>  goog.base(this);
<a name="line45"></a>
<a name="line46"></a>  /**
<a name="line47"></a>   * Underlying IndexedDB transaction object.
<a name="line48"></a>   *
<a name="line49"></a>   * @type {!IDBTransaction}
<a name="line50"></a>   * @private
<a name="line51"></a>   */
<a name="line52"></a>  this.tx_ = tx;
<a name="line53"></a>
<a name="line54"></a>  /**
<a name="line55"></a>   * The database that this transaction modifies.
<a name="line56"></a>   *
<a name="line57"></a>   * @type {!goog.db.IndexedDb}
<a name="line58"></a>   * @private
<a name="line59"></a>   */
<a name="line60"></a>  this.db_ = db;
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * Event handler for this transaction.
<a name="line64"></a>   *
<a name="line65"></a>   * @type {!goog.events.EventHandler}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.eventHandler_ = new goog.events.EventHandler(this);
<a name="line69"></a>
<a name="line70"></a>  // TODO(user): remove these casts once the externs file is updated to
<a name="line71"></a>  // correctly reflect that IDBTransaction extends EventTarget
<a name="line72"></a>  this.eventHandler_.listen(
<a name="line73"></a>      /** @type {EventTarget} */ (this.tx_),
<a name="line74"></a>      &#39;complete&#39;,
<a name="line75"></a>      goog.bind(
<a name="line76"></a>          this.dispatchEvent,
<a name="line77"></a>          this,
<a name="line78"></a>          goog.db.Transaction.EventTypes.COMPLETE));
<a name="line79"></a>  this.eventHandler_.listen(
<a name="line80"></a>      /** @type {EventTarget} */ (this.tx_),
<a name="line81"></a>      &#39;abort&#39;,
<a name="line82"></a>      goog.bind(
<a name="line83"></a>          this.dispatchEvent,
<a name="line84"></a>          this,
<a name="line85"></a>          goog.db.Transaction.EventTypes.ABORT));
<a name="line86"></a>  this.eventHandler_.listen(
<a name="line87"></a>      /** @type {EventTarget} */ (this.tx_),
<a name="line88"></a>      &#39;error&#39;,
<a name="line89"></a>      this.dispatchError_);
<a name="line90"></a>};
<a name="line91"></a>goog.inherits(goog.db.Transaction, goog.events.EventTarget);
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Dispatches an error event based on the given event, wrapping the error
<a name="line96"></a> * if necessary.
<a name="line97"></a> *
<a name="line98"></a> * @param {Event} ev The error event given to the underlying IDBTransaction.
<a name="line99"></a> * @private
<a name="line100"></a> */
<a name="line101"></a>goog.db.Transaction.prototype.dispatchError_ = function(ev) {
<a name="line102"></a>  if (ev.target instanceof goog.db.Error) {
<a name="line103"></a>    this.dispatchEvent({
<a name="line104"></a>      type: goog.db.Transaction.EventTypes.ERROR,
<a name="line105"></a>      target: ev.target
<a name="line106"></a>    });
<a name="line107"></a>  } else {
<a name="line108"></a>    this.dispatchEvent({
<a name="line109"></a>      type: goog.db.Transaction.EventTypes.ERROR,
<a name="line110"></a>      target: goog.db.Error.fromRequest(
<a name="line111"></a>          /** @type {!IDBRequest} */ (ev.target), &#39;in transaction&#39;)
<a name="line112"></a>    });
<a name="line113"></a>  }
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Event types the Transaction can dispatch. COMPLETE events are dispatched
<a name="line119"></a> * when the transaction is committed. If a transaction is aborted it dispatches
<a name="line120"></a> * both an ABORT event and an ERROR event with the ABORT_ERR code. Error events
<a name="line121"></a> * are dispatched on any error.
<a name="line122"></a> *
<a name="line123"></a> * @enum {string}
<a name="line124"></a> */
<a name="line125"></a>goog.db.Transaction.EventTypes = {
<a name="line126"></a>  COMPLETE: &#39;complete&#39;,
<a name="line127"></a>  ABORT: &#39;abort&#39;,
<a name="line128"></a>  ERROR: &#39;error&#39;
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * @return {goog.db.Transaction.TransactionMode} The transaction&#39;s mode.
<a name="line134"></a> */
<a name="line135"></a>goog.db.Transaction.prototype.getMode = function() {
<a name="line136"></a>  return /** @type {goog.db.Transaction.TransactionMode} */ (this.tx_.mode);
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * @return {!goog.db.IndexedDb} The database that this transaction modifies.
<a name="line142"></a> */
<a name="line143"></a>goog.db.Transaction.prototype.getDatabase = function() {
<a name="line144"></a>  return this.db_;
<a name="line145"></a>};
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * Opens an object store to do operations on in this transaction. The requested
<a name="line150"></a> * object store must be one that is in this transaction&#39;s scope.
<a name="line151"></a> * @see goog.db.IndexedDb#createTransaction
<a name="line152"></a> *
<a name="line153"></a> * @param {string} name The name of the requested object store.
<a name="line154"></a> * @return {!goog.db.ObjectStore} The wrapped object store.
<a name="line155"></a> * @throws {goog.db.Error} In case of error getting the object store.
<a name="line156"></a> */
<a name="line157"></a>goog.db.Transaction.prototype.objectStore = function(name) {
<a name="line158"></a>  try {
<a name="line159"></a>    return new goog.db.ObjectStore(this.tx_.objectStore(name));
<a name="line160"></a>  } catch (ex) {
<a name="line161"></a>    throw goog.db.Error.fromException(ex, &#39;getting object store &#39; + name);
<a name="line162"></a>  }
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * @return {!goog.async.Deferred} A deferred that will fire once the
<a name="line168"></a> *     transaction is complete. It fires the errback chain if an error occurs
<a name="line169"></a> *     in the transaction, or if it is aborted.
<a name="line170"></a> */
<a name="line171"></a>goog.db.Transaction.prototype.wait = function() {
<a name="line172"></a>  var d = new goog.async.Deferred();
<a name="line173"></a>  goog.events.listenOnce(
<a name="line174"></a>      this, goog.db.Transaction.EventTypes.COMPLETE, goog.bind(d.callback, d));
<a name="line175"></a>  goog.events.listenOnce(
<a name="line176"></a>      this, goog.db.Transaction.EventTypes.ABORT, function() {
<a name="line177"></a>        d.errback(new goog.db.Error(goog.db.Error.ErrorCode.ABORT_ERR,
<a name="line178"></a>            &#39;waiting for transaction to complete&#39;));
<a name="line179"></a>      });
<a name="line180"></a>  goog.events.listenOnce(
<a name="line181"></a>      this, goog.db.Transaction.EventTypes.ERROR, function(e) {
<a name="line182"></a>        d.errback(e.target);
<a name="line183"></a>      });
<a name="line184"></a>
<a name="line185"></a>  var db = this.getDatabase();
<a name="line186"></a>  return d.addCallback(function() {
<a name="line187"></a>    return db;
<a name="line188"></a>  });
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>/**
<a name="line193"></a> * Aborts this transaction. No pending operations will be applied to the
<a name="line194"></a> * database. Dispatches an ABORT event.
<a name="line195"></a> */
<a name="line196"></a>goog.db.Transaction.prototype.abort = function() {
<a name="line197"></a>  this.tx_.abort();
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/** @override */
<a name="line202"></a>goog.db.Transaction.prototype.disposeInternal = function() {
<a name="line203"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line204"></a>  this.eventHandler_.dispose();
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * The three possible transaction modes.
<a name="line210"></a> * @see http://www.w3.org/TR/IndexedDB/#idl-def-IDBTransaction
<a name="line211"></a> *
<a name="line212"></a> * @enum {string}
<a name="line213"></a> */
<a name="line214"></a>goog.db.Transaction.TransactionMode = {
<a name="line215"></a>  READ_ONLY: &#39;readonly&#39;,
<a name="line216"></a>  READ_WRITE: &#39;readwrite&#39;,
<a name="line217"></a>  VERSION_CHANGE: &#39;versionchange&#39;
<a name="line218"></a>};
</pre>


</body>
</html>
